package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.AccountRuleForm;
import com.woniuxy.controller.info.AccountRuleInfo;
import com.woniuxy.dao.RoleDao;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.RuleNotFountException;
import com.woniuxy.model.AccountRole;
import com.woniuxy.dao.AccountRoleDao;
import com.woniuxy.model.Role;
import com.woniuxy.service.AccountRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-29
 */
@Service
public class AccountRoleServiceImpl extends ServiceImpl<AccountRoleDao, AccountRole> implements AccountRoleService {


    @Resource
    private AccountRoleDao accountRuleDao;
    @Resource
    private RoleDao ruleDao;
    @Override
    public AccountRuleInfo findRuleIdByAccountId(Integer id) {
        QueryWrapper<AccountRole> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id",id);
        List<AccountRole> list = accountRuleDao.selectList(wrapper);
        List<Integer> ids = new ArrayList<>();
        AccountRuleInfo accountRuleInfo = new AccountRuleInfo();
        for (AccountRole ar:list){
            Role rule = ruleDao.selectById(ar.getRoleId());
            ids.add(rule.getRoleId());
            accountRuleInfo.setRuleIds(ids);
        }
        System.out.println(accountRuleInfo);
        return accountRuleInfo;
    }

    @Override
    @Transactional(rollbackFor = DdException.class)
    public Boolean updateAccountRule(AccountRuleForm arf) throws DdException {
        QueryWrapper<AccountRole> wrapper = new QueryWrapper<AccountRole>().eq("account_id", arf.getAccountId());
        List<AccountRole> accountRules = accountRuleDao.selectList(wrapper);
        //如果为null,就直接添加，如果不为null,就先移除在添加
        if(accountRules==null&&accountRules.size()<=0){
            List<Integer> ruleIds = arf.getRuleIds();
            if(ruleIds.size()<=0){
                accountRuleDao.delete(wrapper);
            }
            AccountRole accountRule = new AccountRole();
            accountRule.setAccountId(arf.getAccountId());
            Integer count = ruleDao.selectCount(null);
            for(Integer i:ruleIds){
                if(i>count){
                    throw new RuleNotFountException(50001,"角色不存在");
                }
                accountRule.setRoleId(i);
                accountRuleDao.insert(accountRule);
            }
        }else{//如果不为null,先移除
            accountRuleDao.delete(wrapper);
            //再添加
            List<Integer> ruleIds = arf.getRuleIds();
            if(ruleIds.size()<=0){
                accountRuleDao.delete(wrapper);
            }
            AccountRole accountRule = new AccountRole();
            accountRule.setAccountId(arf.getAccountId());
            Integer count = ruleDao.selectCount(null);
            for(Integer i:ruleIds){
                if(i>count){
                    throw new RuleNotFountException(50001,"角色不存在");
                }
                accountRule.setRoleId(i);
                accountRuleDao.insert(accountRule);
            }
        }
        return true;
    }
}
